<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>图书编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="bookForm">
    <input type="hidden" name="id"  class="layui-input">
    <div class="layui-form-item">
        <label class="layui-form-label required">书籍名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="书籍名不能为空" placeholder="请输入书籍名" value="" class="layui-input">
            <tip>填写书籍名。</tip>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <select name="tid" lay-verify="" id="typeSel">

            </select>
        </div>
    </div>

    <!--    <div class="layui-form-item layui-form-text">-->
    <!--        <label class="layui-form-label">简介</label>-->
    <!--        <div class="layui-input-block">-->
    <!--            <textarea name="desri" class="layui-textarea" placeholder="请输入简介信息"></textarea>-->
    <!--        </div>-->
    <!--    </div>-->

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">简介</label>
        <div class="layui-input-block">
            <textarea name="descri" required lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">价格</label>
        <div class="layui-input-block">
            <input type="text" name="price" lay-verify="required" lay-reqtext="价格不能为空" placeholder="请输入价格" value="" class="layui-input">
            <tip>填写价格。</tip>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">作者</label>
        <div class="layui-input-block">
            <input type="text" name="author" lay-verify="required" lay-reqtext="作者不能为空" placeholder="请输入作者" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">出版社</label>
        <div class="layui-input-block">
            <input type="text" name="pub" placeholder="请输入出版社" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">出版日期</label>
        <div class="layui-input-block">
            <input type="text" id="puDateInput" name="pubDate" placeholder="请输入出版日期" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <!--这个存放上传的文件新名字-->
        <input type="hidden" name="img"  class="layui-input" id="imgInput">
        <label class="layui-form-label">图片</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test1">
                <i class="layui-icon">&#xe67c;</i>上传图片
            </button>
            <img id="imgPre">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            upload = layui.upload,
            $ = layui.$;
        laydate.render({
            elem:'#puDateInput'
        })

        form.val('bookForm',[(${book})]);
        $("#imgPre").attr("src","/"+$("#imgInput").val())

        let types=[(${types})];
        let book=[(${book})];
        let myhtml='<option value="">请选择一个书籍类型</option>';
        for (let i=0;i<types.length;i++){
            if(book.tid==types[i].id){
                myhtml+='<option value="'+types[i].id+'"selected="selected">'+types[i].name+'</option>';
            }else
                myhtml+='<option value="'+types[i].id+'">'+types[i].name+'</option>';
        }

        $("#typeSel").html(myhtml);
        form.render('select'); //刷新select选择框渲染

        //文件上传
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '/common/upload/' //上传接口
            ,done: function(res){
                //上传完毕回调
                console.log(res);
                $("#imgInput").val(res.fileName);
                $("#imgPre").attr("src","/"+$("#imgInput").val())
            }
            ,error: function(){
                //请求异常回调
            },
            field:"uimg"
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            // var index = layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // }, function () {
            //
            //     // 关闭弹出层
            //     layer.close(index);
            //
            //     var iframeIndex = parent.layer.getFrameIndex(window.name);
            //     parent.layer.close(iframeIndex);
            //
            // });
            $.ajax({

                url:'/book/edit',
                method:'put',
                data:JSON.stringify(data.field),
                contentType:'application/json',
                success:function (res) {
                    if (res.code==0){
                        //表格重新加载
                        parent.layui.table.reload('currentTableId');
                        //关闭添加弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                }
            })
            return false;
        });

    });
</script>
</body>
</html>